package com.example.news.fragment;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.example.news.MainActivity;
import com.example.news.R;
import com.example.news.adapter.OneAdapter;
import com.example.news.bean.OneBean;
import com.example.news.iteam.Iteam_oneActivity;
import com.example.news.sql.Sql;

import java.util.ArrayList;

public class Fragment_one extends Fragment {
    private ListView one_listView;
    private ArrayList<OneBean>list;
    Sql sql;



    public ListView getOne_listView() {
        return one_listView;
    }

    public void setOne_listView(ListView one_listView) {
        this.one_listView = one_listView;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_one, container, false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        list=new ArrayList<>();
         sql =new Sql(getActivity());
       init();
        one_listView=getActivity().findViewById(R.id.one_listView);
        one_listView.setAdapter(new OneAdapter(list,getActivity()));


        one_listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {//长按事件
                String name=((MainActivity)getActivity()).aa();//获取用户名
                if (name!=null){//判断用户名是否为空
                    //Log.e("asd", list.get(i).getIndex()+name);
                    SQLiteDatabase sqLiteDatabase=sql.getWritableDatabase();//写入数据库
                    ContentValues contentValues=new ContentValues();
                    contentValues.put("i",String.valueOf(list.get(i).getIndex()));
                    contentValues.put("name",name);
                    sqLiteDatabase.insert("infor",null,contentValues);
                    Toast.makeText(getActivity(),"收藏成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getActivity(),"您还未登陆",Toast.LENGTH_SHORT).show();
                }
                return true;//返回true时,长按事件不会触发点击事件
            }
        });


        one_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {//点击事件
                Intent intent=new Intent(getActivity(), Iteam_oneActivity.class);//跳转到新闻显示页面
                intent.putExtra("i",list.get(i).getIndex()+"");//标识
                intent.putExtra("name",list.get(i).getName());//标题
                startActivity(intent);
                //Toast.makeText(getActivity(), ""+list.get(i).getIndex(),Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void init() {//数据
        list.add(new OneBean(R.drawable.s1,R.drawable.s2,R.drawable.s3,100,"新年夜惨剧！美国中部城市3小时发生4起枪案 致4死4伤"));
        list.add(new OneBean(R.drawable.s6,R.drawable.s7,R.drawable.s8,101,"小米手表Color抢先体验：除了长续航，还有潮流元素"));
        list.add(new OneBean(R.drawable.s11,R.drawable.s12,R.drawable.s13,102,"realme真我X50曝光信息汇总：1月7日发"));
        list.add(new OneBean(R.drawable.s16,R.drawable.s17,R.drawable.s18,103,"这些家电都曾是王者 认识的都暴露年龄了"));
        list.add(new OneBean(R.drawable.z6jpg,R.drawable.z7,R.drawable.z8,81,"苹果的新手机？"));
        list.add(new OneBean(R.drawable.z11,R.drawable.z12,R.drawable.z13,82,"取快递都是免费的，快递驿站到底靠什么挣钱？"));
        list.add(new OneBean(R.drawable.z16,R.drawable.z17,R.drawable.z18,83,"未来5G都能干嘛呢，看看这十大应用场景"));
        list.add(new OneBean(R.drawable.t11,R.drawable.t12,R.drawable.t13,88,"湖人又收到坏消息，11+7高塔申请交易，联盟已经介入调查"));
        list.add(new OneBean(R.drawable.t16,R.drawable.t17,R.drawable.t18,89,"世界杯稳了！国足世预赛前迎来重大利好！全亚洲开始嗅到不安？"));
        list.add(new OneBean(R.drawable.t21,R.drawable.t22,R.drawable.t23,90,"丁俊晖冲大师赛第2冠，奥沙利文让他信心百倍，战乔佩里优势明显"));
        list.add(new OneBean(R.drawable.sj4,R.drawable.sj5,R.drawable.sj6,62,"浙江卫视，这次玩砸了吧"));
        list.add(new OneBean(R.drawable.sj7,R.drawable.sj8,R.drawable.sj9,63,"跨年晚会成“卸妆晚会”，离开美颜滤镜，女星们的颜值很真实"));
        list.add(new OneBean(R.drawable.sj10,R.drawable.sj11,R.drawable.sj12,64,"贾乃亮在离婚后在上热搜，网友：这样的男人，用不着同情"));
        list.add(new OneBean(R.drawable.tz4,R.drawable.tz5,R.drawable.tz6,51,"曹德旺大谈中国房地产 还劝大家别再犯傻买房子?"));
        list.add(new OneBean(R.drawable.tz7,R.drawable.tz8,R.drawable.tz9,53,"5年以后，“房奴”和全款买房的人会有什么样的差别？"));
        list.add(new OneBean(R.drawable.tz7,R.drawable.tz10,R.drawable.tz1,54,"中国未来房地产最保值及最佳投资城市是哪些？"));
        list.add(new OneBean(R.drawable.tz11,R.drawable.tz12,R.drawable.tz13,55,"2020年，房价飙升将终结"));
        list.add(new OneBean(R.drawable.yy4,R.drawable.yy5,R.drawable.yy6,21,"一道菜里就能加5种辣椒！无辣不欢的长沙味道，你吃过多少？"));
        list.add(new OneBean(R.drawable.yy13,R.drawable.yy14,R.drawable.yy15,22,"喜讯！环球总评榜发布 长沙获两项大奖"));
        list.add(new OneBean(R.drawable.yy1,R.drawable.yy2,R.drawable.yy3,23,"“你好，2020！”长沙步行街万人共舞跨新年"));
        list.add(new OneBean(R.drawable.yy7,R.drawable.yy8,R.drawable.yy9,24,"长沙这条千年历史古街藏着这么多美食小吃，还有什么理由不去……"));

    }

    public ArrayList<OneBean> getList() {
        return list;
    }

    public void setList(ArrayList<OneBean> list) {
        this.list = list;
    }
}